package com.viewlibrary.water;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.viewlibrary.R;

/**
 * 能量球
 */
public class WaterView extends View {
    private int waterColor;
    private int strokeColor;
    private String content;
    private int contentSize;
    private int contentColor;

    private float radius = 100;
    private float strokeWidth = 1f;

    private float height;

    public WaterView(Context context) {
        this(context, null);
    }

    public WaterView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WaterView);
        waterColor = typedArray.getColor(R.styleable.WaterView_waterColor, Color.parseColor("#c3f593"));
        strokeColor = typedArray.getColor(R.styleable.WaterView_strokeColor, Color.parseColor("#69c78e"));
        strokeWidth=typedArray.getDimension(R.styleable.WaterView_strokeWidth,dpToPx(0.5f));
        contentColor = typedArray.getColor(R.styleable.WaterView_contentColor, Color.parseColor("#69c78e"));
        content = typedArray.getString(R.styleable.WaterView_contentText);
        contentSize = (int) typedArray.getDimension(R.styleable.WaterView_contentSize, spToPx(30));
        typedArray.recycle();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        switch (widthMode) {
            //宽度不确定
            case MeasureSpec.AT_MOST:
                if (heightMode==MeasureSpec.AT_MOST){
                    //高度不确定,确定宽高
                    widthSize= (int) ((radius+strokeWidth)*2);
                    heightSize=widthSize;
                }else {
                    //高度确定,确定宽
                    widthSize=heightSize;
                    radius= (int) (heightSize/2-strokeWidth);
                }
                break;
                //宽度确定
            case MeasureSpec.EXACTLY:
                if (heightMode==MeasureSpec.AT_MOST){
                    //高度不确定,确定高
                    heightSize=widthSize;
                    radius= (int) (widthSize/2-strokeWidth);
                }else {
                    //高度确定
                    radius= (int) (widthSize/2-strokeWidth);
                }
                break;
        }
        height=heightSize;
        setMeasuredDimension(widthSize,heightSize);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制外层空心圆
        drawStrokeCircle(canvas);
        //绘制内层实心圆
        drawCirCle(canvas);
        //绘制文字
        drawText(canvas);
    }

    private void drawText(Canvas canvas) {
        Paint paint=new Paint();
        paint.setTextSize(contentSize);
        paint.setColor(contentColor);
        paint.getTextBounds(content,0,content.length(),new Rect());
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        canvas.drawText(content,paint.measureText(content)/2,height/2-(fontMetrics.bottom+fontMetrics.top)/2,paint);
    }

    private void drawCirCle(Canvas canvas) {
        Paint paint=new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(waterColor);
        canvas.drawCircle(height/2,height/2,height/2,paint);
    }

    private void drawStrokeCircle(Canvas canvas) {
        Paint paint=new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(strokeColor);
        canvas.drawCircle(height/2,height/2,height/2,paint);
    }

    private float dpToPx(float value){
      return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,getResources().getDisplayMetrics());
    }

    private float spToPx(float value){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, value, getResources().getDisplayMetrics());
    }

    public void setContent(String content) {
        this.content = content;
    }
}
